<template>
  <div class="detail">
    <div v-if="articleData && Object.keys(articleData).length !== 0">
      <NavBar></NavBar>
      <scroll class="container">
        <div class="content">
          <div class="user">
            <div class="avatar">
              <img :src="userInfo && userInfo.avatar" alt="">
            </div>
            <div class="nickname">{{nickname}}</div>
            <div class="publish_time">{{time}}</div>
          </div>
          <div class="base_info">
            <div class="firm">
              <span>公司:</span>
              <span>{{articleData.baseInfo.firm}}</span>
            </div>
            <div class="post">
              <span>职位:</span>
              <span>{{articleData.baseInfo.position}}</span>
            </div>
            <div class="address">
              <span></span>
              <span>{{articleData.baseInfo.city}}</span>
            </div>
          </div>
          <div class="feedback">
            <span>面试结果:</span> <i>{{articleData.feedback.result}}</i>
            <span>面试难度:</span> <i>{{articleData.feedback.difficulty}}</i>
            <span>面试感受:</span> <i>{{articleData.feedback.feeling}}</i>
          </div>
          <div class="title">
            <span>标题:</span>
            <span>{{articleData.title}}</span>
          </div>
          <div class="background_message">
            <title-list class="background_message_title" @titleItemClick="titleItemClick"></title-list>
            <scroll class="background_message_container">
              <div class="background_message_content">
                <background v-show="titleIndex === 0" :background="articleData.background"></background>
                <post-desc v-show="titleIndex === 1" :post-desc="articleData.positionInfo.postDesc"></post-desc>
                <post-require v-show="titleIndex === 2" :post-require="articleData.positionInfo.postRequire"></post-require>
                <interview-prepare v-show="titleIndex === 3" :interview-prepare="articleData.interview.interviewPrepare"></interview-prepare>
                <interview-resume v-show="titleIndex === 4" :interview-resume="articleData.interview.interviewResume"></interview-resume>
              </div>
            </scroll>
          </div>
          <div class="interview_detail">
            <div class="interview_detail_process">面试过程:</div>
            <interview-detail :interview-detail-list="articleData.interview.detail"></interview-detail>
          </div>
          <div class="conclude">
            <span>面试总结:</span>
            <span>{{articleData.interview.interviewConsole}}</span>
          </div>
          <div class="action">
            <div class="like" @click="like">
              <span class="iconfont" v-show="!isLike">&#xe8ab;</span>
              <span class="iconfont like_active" v-show="isLike">&#xe8c3;</span>
              <span>点赞</span>
            </div>
            <div class="collect" @click="collect">
              <span class="iconfont" v-show="!isCollect">&#xe8b9;</span>
              <span class="iconfont collect_active" v-show="isCollect">&#xe8c6;</span>
              <span>收藏</span>
            </div>
          </div>
        </div>
        <div class="comment">
          <comment :article-id="articleId" :title="articleData.title"></comment>
          <div class="comment_list">
            <div class="title">共{{articleData.commentList.length}}条评论</div>
            <comment-content v-for="(comment, index) in  articleData.commentList"
                             :comment-content-item="comment"
                             :floor="index"
                             :article-id="articleId">
            </comment-content>
          </div>
        </div>
      </scroll>
    </div>
    <div v-else class="already_del">暂无数据</div>
  </div>
</template>

<script>
  import NavBar from "components/content/navBar/NavBar";
  import Scroll from "components/content/scroll/Scroll";

  import TitleList from "./childComps/TitleList";
  import Background from "./childComps/Background";
  import InterviewPrepare from "./childComps/InterviewPrepare";
  import InterviewResume from "./childComps/InterviewResume";
  import PostDesc from "./childComps/PostDesc";
  import PostRequire from "./childComps/PostRequire";
  import InterviewDetail from "./childComps/interviewDetail/InterviewDetail";
  import Comment from "./childComps/comment/Comment";
  import CommentContent from "./childComps/comment/CommentContent";

  import {getArticle, getUserInfo, GetUserBaseInfo} from "network/detail";
  import {formatTime} from "common/Utils";
  import {isCollect, cancelCollection} from "network/collect";

  export default {
    name: "Detail",
    components: {
      NavBar,
      Scroll,
      TitleList,
      Background,
      InterviewPrepare,
      InterviewResume,
      PostDesc,
      PostRequire,
      InterviewDetail,
      Comment,
      CommentContent,
    },
    data() {
      return {
        articleId: '',
        userInfo: {},
        articleData: {},
        titleIndex: 0,
        isLike: false,
        isCollect: false,
        isLogin: this.$store.state.userInfo.isLogin
      }
    },
    created() {
      this.articleId = this.$route.params.articleId
      new Promise((resolve, reject) => {
        getArticle(this.articleId).then(res => {
          if(res.data.code) {
            this.articleData = res.data.data
            resolve()
          }
        })
      }).then(() => {
        const obj = {
          userId: this.articleData.userId
        }
        getUserInfo(obj).then(res => {
          if(res.data.code) {
            this.userInfo = new GetUserBaseInfo(res.data.data.basicInfo)
          }
        })
      })
      isCollect('articles', this.$store.state.userInfo.id, this.articleId).then(res => {
        if(res.data.code) {
          this.isCollect = res.data.data
        }
      })
    },
    mounted() {
      this.$bus.$on('refresh', () => {
        getArticle(this.articleId).then(res => {
          if(res.data.code) {
            this.articleData = res.data.data
          }
        })
      })
    },
    computed: {
      nickname() {
        return this.articleData.isAnonymous ? '匿名用户' : this.userInfo.name
      },
      time() {
        return this.articleData.publishTime ? formatTime(this.articleData.publishTime) : ''
      }
    },
    methods: {
      titleItemClick(index) {
        this.titleIndex = index
      },
      like() {
        if (!this.isLogin) {
          this.$toast.show('请完成登录，再点赞内容', 'failure', 1000);
          return;
        }
        this.isLike = !this.isLike;
      },
      collect() {
        if (!this.isLogin) {
          this.$toast.show('请完成登录，再收藏内容', 'failure', 1000);
          return;
        }
        // 获取到当前面经的信息，以及当前用户的信息，将面经收藏在用户的收藏列表中
        if(!this.isCollect) {
          this.$messageBox.collect(this.articleId, this.$store.state.userInfo.id, 'articles')
          this.isCollect = true
        } else {
          const obj = {
            userId: this.$store.state.userInfo.id,
            type: "articles",
            collectId: this.articleId
          }
          cancelCollection(obj).then(res => {
            if(res.data.code) {
              this.$toast.show('已取消收藏', 'success', 1000)
              this.isCollect = false
            }
          })

        }
      },
    },
  }
</script>

<style scoped>
  .detail {
    margin: 64px auto 0;
    box-sizing: border-box;
  }

  .already_del {
    font-size: 20px;
    text-align: center;
    color: #8F9395;
  }

  .container {
    width: 100%;
    height: calc(100vh - 64px);

  }

  .content {
    width: 1100px;
    padding: 0 40px;
    margin: 0 auto;
    box-sizing: border-box;
    background-color: #fff;
  }

  .user {
    height: 50px;
    padding: 10px 0;
    line-height: 50px;
  }

  .user .avatar {
    float: left;
    width: 50px;
    height: 50px;
    margin-right: 20px;
    border-radius: 25px;
    overflow: hidden;
  }

  .user .avatar img {
    width: 50px;
    height: 50px;
  }

  .user .nickname {
    float: left;
    margin-right: 20px;
    font-size: 16px;
    font-weight: 700;
  }

  .user .publish_time {
    float: left;
    font-size: 14px;
    color: #8F9395;
  }

  .base_info {
    overflow: hidden;
    height: 30px;
    line-height: 30px;
  }

  .base_info > div {
    float: left;
    margin-right: 50px;
    font-size: 14px;
  }

  .base_info .firm span:nth-child(1),
  .base_info .post span:nth-child(1) {
    margin-right: 10px;
  }

  .base_info .address span:nth-child(1) {
    margin-right: 2px;
    font-family: icomoon;
    color: #002766;
  }

  .feedback {
    height: 30px;
    line-height: 30px;
    font-size: 14px;
  }

  .feedback span {
    margin-right: 10px;
    color: #8F9395;
  }

  .feedback i {
    position: relative;
    margin-right: 50px;
    font-style: normal;
  }

  .feedback i:nth-of-type(1)::after,
  .feedback i:nth-of-type(2)::after {
    position: absolute;
    top: -6px;
    right: -28px;
    content: "|";
    color: #8F9395;
  }

  .title {
    margin: 15px 0;
    font-size: 20px;
    font-weight: 700;
  }

  .title span:nth-child(1) {
    margin-right: 10px;
  }

  .background_message {
    overflow: hidden;
    margin-top: 25px;
    padding: 10px;
  }

  .background_message .background_message_title {
    float: left;
    margin-right: 20px;
  }

  .background_message .background_message_container {
    float: left;
    width: 850px;
    height: 200px;
    padding: 20px;
    box-sizing: border-box;
    border-radius: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0,0,0, .05);
  }

  .interview_detail {
    margin: 10px auto;
  }

  .interview_detail_process {
    padding-left: 5px;
    font-size: 18px;
    font-weight: 700;
  }

  .conclude {
    overflow: hidden;
    padding: 20px;
    background-color: #fff;
    border-radius: 10px;
  }

  .conclude span {
    float: left;
  }

  .conclude span:nth-child(1) {
    margin-right: 10px;
    padding-left: 5px;
    font-size: 18px;
    font-weight: 700;
  }

  .conclude span:nth-child(2) {
    width: 860px;
    margin-top: -2px;
    font-size: 14px;
    line-height: 1.7;
  }

  .action {
    overflow: hidden;
  }

  .action .like,
  .action .collect {
    float: right;
    width: 70px;
    height: 32px;
    margin-right: 30px;
    border: 1px solid #DCDFE6;
    border-radius: 5px;
    line-height: 32px;
    text-align: center;
    font-size: 14px;
    cursor: pointer;
  }

  .iconfont {
    margin-right: 2px;
  }

  .like_active {
    color: #002766;
  }

  .collect_active {
    color: #002766;
  }

  .comment {
    width: 1100px;
    margin: 0 auto;
    padding: 40px 60px;
    background-color: #fff;
    box-sizing: border-box;
  }


</style>
